package io.renren.controller;

import io.renren.annotation.Login;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.entity.FaqEntity;
import io.renren.service.FaqService;
import io.renren.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

/**
 * 客服接口
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("/api/service")
@Api(tags = "客服接口")
public class ApiServiceController extends AbstractController {

	@Autowired
	private NoticeService noticeService;

	@Autowired
	private FaqService faqService;
	
	@Login
	@PostMapping("notice")
	@ApiOperation("系统公告")
	public R list(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageUtils page = noticeService.queryPage(params);
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("faq")
	@ApiOperation("新手问答")
	public R listFaq(@ApiIgnore @RequestParam Map<String, Object> params) {
		PageUtils page = faqService.queryPage(params);
		for (Object o : page.getList()) {
			FaqEntity entity = (FaqEntity) o;
			if (entity.getContent() != null && entity.getContent().length() > 30) {
				entity.setContent(entity.getContent().substring(0, 30) + "...");
			}
		}
		return R.ok().put("page", page);
	}

	@Login
	@PostMapping("faqDetail")
	@ApiOperation("新手问答详情")
	public R faqDetail(@RequestParam("id") Long id) {
		return R.ok().put("faq", faqService.selectById(id));
	}
}
